/**
 * 北京百年育人教育投资有限公司 版权所有
 * <p/>
 * 文件:com.easemob.chatuidemo.db.PushMessageDao.java
 * <p/>
 * 创建时间:2015年6月8日
 * <p/>
 * 创建人:马海明
 */
package com.sxtyxt.online.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.sxtyxt.entity.Goods;
import com.sxtyxt.online.App;
import com.sxtyxt.util.DateUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 浏览记录dao
 *
 * @author 马海明
 * @created 马海明 2015年6月8日
 */
public class LookHistoryListDao {
    public static final String TABLE_NAME = "look_history";
    public static final String COLUMN_NAME_ID = "id";
    public static final String COLUMN_NAME_GOODS_ID = "goodsId";
    public static final String COLUMN_NAME_GOODS_NAME = "goodsName";
    public static final String COLUMN_NAME_GOODS_PRICE = "goodsPrice";
    public static final String COLUMN_NAME_GOODS_IMG = "goodsImg";
    public static final String COLUMN_NAME_CREATE = "createTime";
    public static final String COLUMN_NAME_EXT1 = "ext1";
    public static final String COLUMN_NAME_EXT2 = "ext2";
    public static final String COLUMN_NAME_EXT3 = "ext3";
    private static DbHelper dbHelper;
    public static final String DROP = "DROP TABLE IF EXISTS " + TABLE_NAME;


    public static SQLiteDatabase getDB() {
        if (dbHelper == null) {
            dbHelper = DbHelper.getInstance(App.getInstance());
        }
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        return db;
    }

    /**
     * 获取消息总条数
     *
     * @return
     */
    public static int getMessageRows() {
        Cursor cursor = getDB().rawQuery("Select count(*) rows from " + TABLE_NAME, null);
        if (cursor != null && cursor.moveToFirst()) {
            return cursor.getInt(cursor.getColumnIndexOrThrow("rows"));
        }
        return 0;
    }

    /**
     * @return
     */
    public static List<Goods> getGoodsList() {
        List<Goods> msgList = new ArrayList<Goods>();
        if (getDB().isOpen()) {
            Cursor cursor = getDB().query(TABLE_NAME, null, null, null, null, null, COLUMN_NAME_CREATE + " DESC");
            while (cursor.moveToNext()) {
                String id = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_GOODS_ID));
                String goodsImg = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_GOODS_IMG));
                String goodsName = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_GOODS_NAME));
                String goodsPrice = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_GOODS_PRICE));
                Goods msg = new Goods();
                msg.setGoodsId(id);
                msg.setGoodsStorePrice(goodsPrice);
                msg.setGoodsImage(goodsImg);
                msg.setGoodsName(goodsName);
                msgList.add(msg);
            }
            cursor.close();
        }
        return msgList;
    }

    public static void deleteAll() {
        if (getDB().isOpen()) {
            getDB().delete(TABLE_NAME, null, null);
        }
    }

    /**
     * 保存一个消息
     *
     * @param msg
     */
    public static void saveMessage(Goods msg) {
        if (msg == null) {
            System.out.println("msg is null");
            return;
        }
        ContentValues values = new ContentValues();

        if (msg.getGoodsId() != null) {
            values.put(COLUMN_NAME_GOODS_ID, msg.getGoodsId());
        }
        if (msg.getGoodsImage() != null) {
            values.put(COLUMN_NAME_GOODS_IMG, msg.getGoodsImage());
        }

        if (msg.getGoodsName() != null) {
            values.put(COLUMN_NAME_GOODS_NAME, msg.getGoodsName());
        }
        values.put(COLUMN_NAME_GOODS_PRICE, msg.getGoodsStorePrice() + "");
        values.put(COLUMN_NAME_CREATE, DateUtil.getCurrentDay());

        if (findMessageById(msg.getGoodsId())) {
            getDB().update(TABLE_NAME, values, COLUMN_NAME_GOODS_ID + "=?", new String[]{msg.getGoodsId()});
            System.out.println("update success");
        } else {
            getDB().replace(TABLE_NAME, null, values);
            System.out.println("insert success");
        }
    }


    public static  boolean findMessageById(String goodsId) {
        System.out.println(goodsId + "");
        if (goodsId == null) {
            return false;
        }
        Cursor cursor = getDB().rawQuery("select * from " + TABLE_NAME + " where " + COLUMN_NAME_GOODS_ID + " = ?",
                new String[]{goodsId});
        if (cursor != null && cursor.moveToFirst()) {
            return true;
        }
        return false;
    }
}
